<?php
//рус
class cfg_admin_http_web extends controller_prototype_class
{
	private $_id;
	private $_idParts = array();
	protected $_paramsCtrl;
	
	protected $_paramsFieldName = 'cfg_params';
	
    protected function ControllerInit ()
    {

    }    
    
    protected function PrimaryActionInit ($basicActionName)
    {
    	$key = $this->GetQueryIdParts(0);
		$this->_module->GetDecorator()->SetMenuActiveElement('cfg/cfg_' . $key); 
    }

    public function GetQueryId()
    {
    	if ($this->_id !== null) return $this->_id;

		$this->_id = $this->Request()->get('id');	
		$this->_idParts = explode('/', $this->_id);
		
		$fields = MozgCfg::GetProp('admin/cfg//fields');
		for ($i=0; $i<sizeof($this->_idParts); $i++)
		{
			if (!isset($fields[$this->_idParts[$i]]))
			{
				$this->_idParts[$i] = array_shift(array_keys($fields));
			}
			
			$fields = $fields[$this->_idParts[$i]]['subcfg'];
		}
		
		$this->_id = implode('/', $this->_idParts);	
		
		return $this->_id; 
    }
    
    public function GetQueryIdParts($key = false)
    {
    	$this->GetQueryId();
    	
    	if (false === $key) return $this->_idParts; 
    	return isset($this->_idParts[$key]) ? $this->_idParts[$key] : false;
    }

    public function GetMenuElements()
    {
    	$fields = MozgCfg::GetProp('admin/cfg//fields');
    	
    	$links = array();
    	foreach ($fields as $key => $value)
    	{
    		$links['cfg_' .$key] = array(
    				'title' => $value['name'],
    				'link' => array(
    							'module' => 'admin',
    							'ctrl' => 'cfg',
    							'action' => 'out_cfg',
    							'id' => $key,
    				),
    		);
    	}

        return $links;
    }          
    
	protected function GetParamsPath($id)
	{
		$parts = explode('/', $id);
		$field = array_pop($parts);
		
		$fields = MozgCfg::GetProp('admin/cfg//fields');
		if (sizeof($parts))
		{
			foreach ($parts as $part)
			{
				$fields = $fields[$part]['subcfg'];
			}
		}	

		return $fields[$field]['fieldparams'];
	}    
    
    public function GetParamsCtrl()
    {
    	if (!$this->_paramsCtrl)
    	{
	        $this->_paramsCtrl = SysLoader::GetModule('controlls', 'http', '_web')->GetCtrl('params')
	        																		->SetFieldArrName($this->_paramsFieldName);    	    					   		
    	}

    	return $this->_paramsCtrl;
    }      
    
	/**
		экшен по-умолчанию
	*/
    public function PrAction ()
    {
        $this->out_cfgPr();
    }
    public function save_cfgPrAction ()
    {
    	$id = $this->GetQueryId();
    	$config = MozgCfg::Get($this->GetParamsPath($id));
    	
		$config->SetData((array)$this->Request()->getPost($this->_paramsFieldName));
		$config->SetData($this->GetParamsCtrl()->PreparePostData($config));
    	
		MozgCfg::Save($this->GetParamsPath($id));
		
        MozgRoute::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'cfg', 'action' => 'out_cfg', 'id' => $id));
    }
    public function out_cfgPrAction ()
    {
        $t = $this->GetTemplator();
        $t->id = $this->GetQueryId();

        $t->paramsBlock = $this->GetParamsCtrl()->GetFullHtml(MozgCfg::Get($this->GetParamsPath($t->id)));
        $t->paramFieldsJS = $this->GetParamsCtrl()->GetParamsFieldsJS();      	

        $this->LoadTemplate ($t->render('cfg/_sitecfg.phtml'));
        $this->GetModule()->GetDecorator()->SetSecondAction('Left Sidebar', 'OutCfgSubmenuInBlock', 'cfg'); 
    }
    protected function LoadTemplate ($content)
    {
        $t = $this->GetTemplator();
        $t->title = 'Настройки';
        $t->content = $content;
        $this->Response()->SetBody($t->render('_main_block_template.phtml'));
    }  

    public function OutCfgSubmenu()
    {
    	$fields = MozgCfg::GetProp('admin/cfg//fields');
    	
    	if (!isset($fields[$this->GetQueryIdParts(0)]['subcfg'])) return '';
    	
		$subFields = $fields[$this->GetQueryIdParts(0)]['subcfg'];
    	if (!sizeof($subFields)) return '';
		
        $t = $this->GetTemplator();
        $t->id = $this->GetQueryId();
  		$t->idParts = $this->GetQueryIdParts();
        $t->fields = $subFields;

        return $t->render('cfg/_submenu.phtml');
    }
    
    public function OutCfgSubmenuInBlock()
    {
    	$content = $this->OutCfgSubmenu();
    	if (!$content) return '';
    	
        $t = $this->GetTemplator();
        $t->title = 'Рзделы настроек';
        $t->content = $content;
        return $t->render('_left_block_template.phtml');    	
    }    
}